package com.zhong.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.zhong.subject.application.convert.SubjectAnswerDTOConverter;
import com.zhong.subject.application.convert.SubjectInfoDTOConverter;
import com.zhong.subject.application.dto.SubjectInfoDTO;
import com.zhong.subject.common.entity.PageResult;
import com.zhong.subject.common.entity.Result;
import com.zhong.subject.domain.bo.SubjectAnswerBO;
import com.zhong.subject.domain.bo.SubjectInfoBO;
import com.zhong.subject.domain.service.SubjectInfoDomainService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 题目controller
 */
@Slf4j
@RestController
@RequestMapping("/subject")
public class SubjectController {

    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    /**
     * 新增题目
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO){
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.add.subjectInfoDTO：{}", JSON.toJSONString(subjectInfoDTO));
            }
            //数据校验
            Preconditions.checkArgument(!StringUtils.isBlank(subjectInfoDTO.getSubjectName()),"题目不能为空！！！");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(),"题目难度不能为空！！！");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(),"题目类型不能为空！！！");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(),"题目分数不能为空！！！");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIds()),"题目分类id不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getLabelIds()),"题目标签id不能为空");
            //数据转换
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
            List<SubjectAnswerBO> subjectAnswerBOList = SubjectAnswerDTOConverter.INSTANCE.convertDTOListToBOList(subjectInfoDTO.getOptionList());
            subjectInfoBO.setOptionList(subjectAnswerBOList);
            //业务
            subjectInfoDomainService.add(subjectInfoBO);
            return Result.ok();
        }catch (Exception e){
            log.error("SubjectController.add.err：{}",e.getMessage(),e);
            return Result.fail("新增题目失败！！！");
        }
    }

    /**
     * 查询题目列表
     */
    @GetMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectController.getSubjectPage.subjectInfoDTO: {}",JSON.toJSONString(subjectInfoDTO));
            }
            //数据校验
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(),"分类id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(),"标签id不能为空");
            //数据转换
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
            subjectInfoBO.setPageSize(subjectInfoDTO.getPageSize());
            subjectInfoBO.setPageNo(subjectInfoDTO.getPageNo());
            //业务
            PageResult<SubjectInfoBO> subjectInfoBOPageResult = subjectInfoDomainService.getSubjectPage(subjectInfoBO);
            return Result.ok(subjectInfoBOPageResult);
        }catch (Exception e){
            log.error("SubjectController.getSubjectPage.error: {}",e.getMessage(),e);
            return Result.fail("查询题目列表失败");
        }
    }

    /**
     * 查询题目详情
     */
    @GetMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectController.querySubjectInfo.SubjectInfoDTO: {}",JSON.toJSONString(subjectInfoDTO));
            }
            //数据校验
            Preconditions.checkNotNull(subjectInfoDTO.getId(),"题目id不能为空！！！");

            //数据转换
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);

            //业务
            SubjectInfoBO infoBO = subjectInfoDomainService.querySubjectInfo(subjectInfoBO);
            SubjectInfoDTO infoDTO = SubjectInfoDTOConverter.INSTANCE.convertBoToDTO(infoBO);

            //将不必要的参数隐藏
            infoDTO.setPageNo(null);
            infoDTO.setPageSize(null);

            return Result.ok(infoDTO);
        }catch (Exception e){
            log.error("SubjectController.querySubjectInfo.error: {}",e.getMessage(),e);
            return Result.fail("查询题目详情失败");
        }
    }
}
